import sys
from PyQt6.QtWidgets import (
    QApplication,
    QMainWindow,
    QPushButton,
    QVBoxLayout,
    QWidget,
)
from PyQt6.QtCore import QSize, pyqtSignal, pyqtSlot
from random import choice

window_titles = [
    "My App",
    "My App",
    "Still My App",
    "Still My App",
    "What on earth",
    "What on earth",
    "This is surprising",
    "This is surprising",
    "Something went wrong",
]


class MainWindow(QMainWindow):

    # 自定义信号
    mySignal = pyqtSignal()

    def __init__(self):
        super().__init__()
        self.setWindowTitle("My App")
        self.resize(QSize(400, 300))

        layout = QVBoxLayout()
        container = QWidget()
        container.setLayout(layout)

        self.button1 = QPushButton("Press Me!")
        self.button1.setCheckable(True)
        self.button1.clicked.connect(self.on_button1_clicked)
        self.button1.clicked.connect(self.on_button1_toggled)
        layout.addWidget(self.button1)

        self.button2 = QPushButton("Change Window Title")
        self.button2.clicked.connect(self.on_button2_clicked)
        layout.addWidget(self.button2)

        self.setCentralWidget(container)

        self.mySignal.connect(self.mySlot)

    def on_button1_clicked(self):
        print("Clicked!")
        self.mySignal.emit()

    def on_button1_toggled(self, checked):
        print("Checked?", checked)

    def on_button2_clicked(self):
        new_window_title = choice(window_titles)
        self.setWindowTitle(new_window_title)


app = QApplication(sys.argv)

window = MainWindow()
window.show()

app.exec()
